package com.caotv.wkreport.dbutils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.caotv.wkreport.dao.*;
import com.caotv.wkreport.utils.LoggingUtils;


public class RoomsTableTool {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public static RoomsTableTool getInstance() {
		RoomsTableTool roomsTableTool = new RoomsTableTool();
		roomsTableTool.sessionFactory = HibernateUtil.getSessionFactory();
		return roomsTableTool;
	}

	public Rooms getRoomByID(Integer id) {
		try {
			Session session = sessionFactory.openSession();
			
			Rooms room = (Rooms) session
					.createCriteria(Rooms.class)
					.add(Restrictions.eq("id", id))
					.uniqueResult();
			session.close();
			return room;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get room by id");
			ex.printStackTrace();
			return null;
		}		
	}	
	public List<Rooms> getRoomByName(String name) {
		List<Rooms> list = new ArrayList<Rooms>();
		try {
			Session session = sessionFactory.openSession();
			
			list = session
					.createCriteria(Rooms.class)
					.add(Restrictions.eq("roomname", name))
					.list();
			session.close();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get all rooms");
			ex.printStackTrace();
		}
		return list;
	}
	
	public boolean addRoom(Rooms room) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {	
			session.save(room);
			trans.commit();
			session.close();
		} catch (Exception e) {
			
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when add room");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public void saveOrUpdateRoom2(Rooms room) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		
		try {
		
			session.saveOrUpdate(room);
			trans.commit();
			session.close();
		} catch (Exception e) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when update room");
			e.printStackTrace();
		}
	}
	
	
	public void deleteRoomByID(int roomID) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
	
		try {
		Rooms room = (Rooms) session.get(Rooms.class, roomID);
		session.delete(room);
		
		trans.commit();
		session.close();
		} catch (Exception e) {
		trans.rollback();
		e.printStackTrace();
		}
		}
	
	
	
	public List<Rooms> getAllRooms() {
		List<Rooms> list = new ArrayList<Rooms>();
		try {
			Session session = sessionFactory.openSession();
			
			list = session
					.createCriteria(Rooms.class)
					.list();
			session.close();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get all rooms");
			ex.printStackTrace();
		}
		return list;
	}
	
	public List<Rooms> getAllActiveRooms() {
		List<Rooms> list = new ArrayList<Rooms>();
		try {
			Session session = sessionFactory.openSession();
			list = session
					.createCriteria(Rooms.class)
					.add(Restrictions.eq("status", 1))
					.list();
			session.close();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get all rooms");
			ex.printStackTrace();
		}
		return list;
	}
}
